package com.demodis.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.demodis.domain.Json;
import com.demodis.domain.TblDeterPerson;
import com.demodis.domain.TblUser;
import com.demodis.service.DeterPersonServiceI;
import com.demodis.service.PermissionServiceI;
import com.demodis.service.impl.DeterPersonServiceImpl;
import com.demodis.service.impl.PermissionServiceImpl;
import com.opensymphony.xwork2.ModelDriven;

public class DeterPersonAction extends BaseAction implements ModelDriven<TblDeterPerson> {
	String QDSOURL = "/deterPersonAction!queryDeterO.action";
	String QDSFURL = "/deterPersonAction!queryDeterF.action";
	String QDSAURL = "/deterPersonAction!queryDeterA.action";

	private static final long serialVersionUID = 1970606002251188103L;
	
	DeterPersonServiceI deterPerService = new DeterPersonServiceImpl();
	PermissionServiceI permService = new PermissionServiceImpl();
	TblDeterPerson deterPer = new TblDeterPerson();
	
	@Override
	public String execute() throws Exception {
		TblUser user = super.getCurrentUser();
		if(permService.existPerm(user.getUserId(), QDSAURL)) {
			user.setDeterUrl("A");
		} else if(permService.existPerm(user.getUserId(), QDSFURL)) {
			user.setDeterUrl("F");
		} else if(permService.existPerm(user.getUserId(), QDSOURL)) {
			user.setDeterUrl("O");
		} else {
			user.setDeterUrl("null");
		}
		
		return "success";
	}

	public void query() {
		TblUser user = super.getCurrentUser();
		super.writeJson(deterPerService.get(deterPer, user));
	}
	
	public void getPersSug() {
		super.writeJson(deterPerService.getPersSug(deterPer.getUserId()));
	}
	
	public void getGroupList() {
		TblUser user = super.getCurrentUser();
		super.writeJson(deterPerService.getGroupList(user.getUserId(), user.getGroupId()));
	}
	
	public void perExist() {
		Json json = new Json();
		if(deterPerService.perExist(deterPer.getUserId())) {
	      json.setSuccess(true);
	      json.setMsg("该测评者已经存在！");
	    } else {
	      json.setMsg("该测评者不存在！");
	    }

	    super.writeJson(json);
	}
	
	public void queryDeterStat() {
		TblUser user = super.getCurrentUser();
		super.writeJson(deterPerService.queryDeterStat(deterPer, user.getUserId(), user.getGroupId()));
	}
	
	public void doDeter() {
		Json json = new Json();
		if(deterPerService.isExist(deterPer.getUserId(), super.getCurrentUser().getUserId())) {
			json.setSuccess(false);
			json.setMsg("你已测评过了！");
		} else if(deterPerService.doDeter(deterPer, super.getCurrentUser())) {
	      json.setSuccess(true);
	      json.setMsg("测评成功!");
	    } else {
	      json.setMsg("测评失败!");
	    }

	    super.writeJson(json);
	}
	
	public void picUpload() throws IOException {
		String realPath = ServletActionContext.getServletContext().getRealPath("/imgs/photo");
		System.out.println(realPath);
		if(this.deterPer.getPic() != null) {
			File saveFile = new File(new File(realPath), deterPer.getUsername()+".jpg");
			if(!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(deterPer.getPic(), saveFile);
			System.out.println("上传成功！");
		}
	}
	
	public void add() throws IOException {
		String realPath = ServletActionContext.getServletContext().getRealPath("/imgs/photo");
		if(this.deterPer.getPic() != null) {
			File saveFile = new File(new File(realPath), deterPer.getUserId()+".jpg");
			if(!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(deterPer.getPic(), saveFile);
			deterPer.setPersonPic(deterPer.getUserId()+".jpg");
		}
		
		Json json = new Json();
		if(deterPerService.save(deterPer)) {
	      json.setSuccess(true);
	      json.setMsg("添加成功!");
	    } else {
	      json.setMsg("添加失败!");
	    }

	    super.writeJson(json);
	}
	
	public void modify() throws IOException {
		String realPath = ServletActionContext.getServletContext().getRealPath("/imgs/photo");
		if(this.deterPer.getPic() != null) {
			File saveFile = new File(new File(realPath), deterPer.getUserId()+".jpg");
			if(!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(deterPer.getPic(), saveFile);
			deterPer.setPersonPic(deterPer.getUserId()+".jpg");
		}
		
		Json json = new Json();
		if(deterPerService.modify(deterPer)) {
	      json.setSuccess(true);
	      json.setMsg("修改成功!");
	      json.setObject(deterPer);
	    } else {
	      json.setMsg("修改失败!");
	    }

	    super.writeJson(json);
	}
	
	public void delete() {
		Json json = new Json();
		if(deterPerService.delete(deterPer)) {
	      json.setSuccess(true);
	      json.setMsg("删除成功!");
	    } else {
	      json.setMsg("删除失败!");
	    }

	    super.writeJson(json);
	}
	
	public void getDeterPerInfo() {
		super.writeJson(deterPerService.getDeterPerInfo(deterPer));
	}
	
	public String groupEmpApp() {
		return "groupEmpApp";
	}
	
	/**
	 * 获取职能部门员工成绩
	 * @return
	 */
	public void getGroupEmpApp() {
		super.writeJson(deterPerService.getGroupEmpApp());
	}
	
	/**
	 * 录入职能部门员工评价成绩
	 */
	public void addAppScore() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("userId", Integer.parseInt(ServletActionContext.getRequest().getParameter("userId")));
		map.put("id", Integer.parseInt(ServletActionContext.getRequest().getParameter("id")));
		map.put("groupAppraise", Integer.parseInt(ServletActionContext.getRequest().getParameter("groupAppraise")));
		map.put("reciprocalAppraise", Integer.parseInt(ServletActionContext.getRequest().getParameter("reciprocalAppraise")));
		map.put("firstGroupAppraise", Integer.parseInt(ServletActionContext.getRequest().getParameter("firstGroupAppraise")));
		Json json = new Json();
		if(deterPerService.addAppScore(map)) {
	      json.setSuccess(true);
	      json.setMsg("成绩录入成功!");
	    } else {
	      json.setMsg("成绩录入失败!");
	    }

	    super.writeJson(json);
	}
	
	@Override
	public TblDeterPerson getModel() {
		return deterPer;
	}

}
